package run.core;

import java.util.ArrayList;
import java.util.List;

import run.Cell;
import run.Facet;
import run.Lattice;

public class LESS_THAN extends Cell {
	private Facet left, right, output;
	private ArrayList<Object> internalValue;
	private boolean noInput;
	private Integer leftOp;
	private Integer rightOp;
	
	public LESS_THAN(String labelName) {
		super("LESS_THAN", labelName);
		left = new Facet(".left", this, INPUT_FACET);
		right = new Facet(".right", this, INPUT_FACET);
		output = new Facet(".output", this, OUTPUT_FACET);
		output.addSource(left);
		internalValue = new ArrayList<Object>();
		noInput = false;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Facet> stimulate(Facet stimmer) {
//		System.out.println("LESS_THAN");
		ArrayList<Facet> doneFacets = new ArrayList<Facet>();
		List<Object> op = stimmer.dispatchOutbound();
		
		// pass along NILS
		if (op.get(0) instanceof NIL) {
			output.receive(left, op);
			doneFacets.add(output);
			return doneFacets;
		}
		
		
		if (stimmer == left)
			leftOp = (Integer) op.get(0);
		else 
			rightOp = (Integer) op.get(0);

		if (leftOp != null && rightOp != null) {
		
			List<Object> returns = new ArrayList<Object>();
			if (leftOp.intValue() < rightOp.intValue())
				returns.add(new Integer(1));
			else
				returns.add(new NIL());
			
			output.receive(left, returns);
			
			doneFacets.add(output);
			leftOp = rightOp = null;
		}
		return doneFacets;
	}
	public List<Facet> getFacets()
	{
		ArrayList<Facet> temp = new ArrayList<Facet>();
		temp.add(left);
		temp.add(right);
		
		if(noInput)
		{
			return temp;
		}

		temp.add(output);
		return temp;
	}
	
	//Dot printout method
	public String toDot(String prefix)
	{
		String returnString = "";
		returnString += "subgraph " + "cluster_" + cellTypeName + hashCode() + "{\n";
		returnString += "label = \"" + cellTypeName + "\";\n";
		
		//Print my facets
		returnString +=  prefix + cellTypeName + hashCode() + "left  [label=\".left\"];\n";
		returnString += prefix + cellTypeName + hashCode() + "right  [label=\".right\"];\n";
		returnString += prefix + cellTypeName + hashCode() + "output  [label=\".output\"];\n";

		returnString += "}\n";
		return returnString;
	}

}
